<?php
// audio start
	//adding columns start
    add_filter('manage_audios_posts_columns', 'audio_columns_add' );
		function audio_columns_add($columns) {
			$columns['category'] = 'Categories';
			$columns['author'] = 'Author';
			$columns['tag'] = 'Tags';
			return $columns;
    }
    add_action('manage_audios_posts_custom_column', 'audio_columns');
		function audio_columns($name) {
			global $post;
			switch ($name) {
				case 'category':
					$categories = get_the_terms( $post->ID, 'audio-category' );
						if($categories <> ""){
							$couter_comma = 0;
							foreach ( $categories as $category ) {
								echo $category->name;
								$couter_comma++;
								if ( $couter_comma < count($categories) ) {
									echo ", ";
								}
							}
						}
					break;
				case 'author':
					echo get_the_author();
					break;
				case 'tag':
					$categories = get_the_terms( $post->ID, 'audio-tag' );
						if($categories <> ""){
							$couter_comma = 0;
							foreach ( $categories as $category ) {
								echo $category->name;
								$couter_comma++;
								if ( $couter_comma < count($categories) ) {
									echo ", ";
								}
							}
						}
					break;
			}
		}
	//adding columns end
	
	function cs_audio_register() {  
		$labels = array(
			'name' => __('Quản lý Audio','mytheme'),
			'add_new_item' => __('Thêm Mới Audio','mytheme'),
			'edit_item' => __('Sửa Audio','mytheme'),
			'new_item' => __('Audio mới','mytheme'),
			'add_new' => __('Thêm Mới Audio','mytheme'),
			'view_item' => __('Hiển thị Audio này','mytheme'),
			'search_items' => __('Tìm Kiếm Audio','mytheme'),
			'not_found' =>  __('Không tìm thấy','mytheme'),
			'not_found_in_trash' => __('không tìm thấy trong thùng rác','mytheme'),
			'parent_item_colon' => ''
		);
		$args = array(
			'labels' => $labels,
			'public' => true,
			'publicly_queryable' => true,
			'show_ui' => true,
			'query_var' => true,
			'menu_icon' => get_stylesheet_directory_uri() . '/images/admin/audio-icon.png',
			'rewrite' => true,
			'capability_type' => 'post',
			'hierarchical' => false,
			'menu_position' => null,
			'supports' => array('title','editor','thumbnail', 'comments','excerpt')
		); 
                 register_post_type( 'audios' , $args );  
        }
	add_action('init', 'cs_audio_register');

	function cs_audio_categories() 
	{
		  $labels = array(
			'name' => __( 'Audio Categories','mytheme' ),
			'search_items' =>  __( 'Search Audio Categories' ,'mytheme'),
			'edit_item' => __( 'Edit Audio Category','mytheme' ), 
			'update_item' => __( 'Update Audio Category','mytheme' ),
			'add_new_item' => __( 'Add New Category','mytheme' ),
			'menu_name' => __( 'Audio Categories' ,'mytheme'),
		  ); 	
		  register_taxonomy('audio-category',array('audios'), array(
			'hierarchical' => true,
			'labels' => $labels,
			'show_ui' => true,
			'query_var' => true,
			'rewrite' => array( 'slug' => 'audio-category' ),
		  ));
	}
	add_action( 'init', 'cs_audio_categories');

	function cs_audio_tag() 
	{
		  $labels = array(
			'name' => __( 'Audio Tags','mytheme'),
			'singular_name' => __( 'audio-tag','mytheme' ),
			'search_items' =>  __( 'Search Tags' ,'mytheme'),
			'popular_items' => __( 'Popular Tags','mytheme' ),
			'all_items' => __( 'All Tags','mytheme' ),
			'parent_item' => null,
			'parent_item_colon' => null,
			'edit_item' => __( 'Edit Tag','mytheme' ), 
			'update_item' => __( 'Update Tag','mytheme' ),
			'add_new_item' => __( 'Add New Tag','mytheme' ),
			'new_item_name' => __( 'New Tag Name' ,'mytheme'),
			'separate_items_with_commas' => __( 'Separate writers with commas','mytheme' ),
			'add_or_remove_items' => __( 'Add or remove tags','mytheme' ),
			'choose_from_most_used' => __( 'Choose from the most used tags','mytheme' ),
			'menu_name' => __( 'Audio Tags','mytheme' ),
		  ); 
		
		  register_taxonomy('audio-tag','audios',array(
			'hierarchical' => false,
			'labels' => $labels,
			'show_ui' => true,
			'update_count_callback' => '_update_post_term_count',
			'query_var' => true,
			'rewrite' => array( 'slug' => 'audio-tag' ),
		  ));
	}
	add_action( 'init', 'cs_audio_tag');

	// adding audio meta info start
		add_action( 'add_meta_boxes', 'cs_meta_audio_add' );
		function cs_meta_audio_add()
		{  
			add_meta_box( 'cs_meta_audio', 'Audio Options', 'cs_meta_audio', 'audios', 'normal', 'high' );  
		}
		function cs_meta_audio( $post ) {
			$cs_audio = get_post_meta($post->ID, "cs_audio", true);
			if ( $cs_audio <> "" ) {
				$xmlObject = new SimpleXMLElement($cs_audio);
					$audio_release_date_db = $xmlObject->audio_release_date;
					$audio_social_share_db = $xmlObject->audio_social_share;
					$audio_buy_amazon_db = $xmlObject->audio_buy_amazon;
					$audio_buy_apple_db = $xmlObject->audio_buy_apple;
					$audio_buy_groov_db = $xmlObject->audio_buy_groov;
					$audio_buy_cloud_db = $xmlObject->audio_buy_cloud;
			}
			else {
				$audio_release_date_db = '';
				$audio_social_share_db = '';
				$audio_buy_amazon_db = '';
				$audio_buy_apple_db = '';
				$audio_buy_groov_db = '';
				$audio_buy_cloud_db = '';
			}
?>
            <div class="page-wrap page-opts left" style="overflow:hidden; position:relative;">
            <script type="text/javascript" src="<?php echo get_template_directory_uri()?>/scripts/jquery.scrollTo-min.js"></script>
                <div class="option-sec" style="margin-bottom:0;">
                    <div class="opt-head">
                        <h6>Release Date and Social Sharing</h6>
                    </div>
                    <div class="opt-conts">
                    	<ul class="form-elements">
                            <li class="to-label">
                                <label>Release Date</label>
                            </li>
                            <li class="to-field">
            
                                    <!--date picker start-->
                                        <link rel="stylesheet" href="<?php echo get_template_directory_uri()?>/scripts/date_range/jquery.ui.datepicker.css">
                                        <link rel="stylesheet" href="<?php echo get_template_directory_uri()?>/scripts/date_range/jquery.ui.theme.css">
                                        <script>
                                        $(function() {
                                            $( "#audio_release_date" ).datepicker({
                                                defaultDate: "+1w",
                                                changeMonth: true,
                                                numberOfMonths: 1,
                                                //onSelect: function( selectedDate ) {
                                                    //$( "#cs_event_to_date" ).datepicker( "option", "minDate", selectedDate );
                                                //}
                                            });
                                        });
                                        </script>
                                    <!--date picker end-->
                                <input type="text" id="audio_release_date" name="audio_release_date" value="<?php if ($audio_release_date_db=="") echo gmdate("Y-m-d"); else echo $audio_release_date_db?>" />
                            </li>
                            <li class="to-desc">
                                <p>
                                    Put audio release date
                                </p>
                            </li>
                        </ul>
                        <ul class="form-elements noborder">
                            <li class="to-label">
                                <label>Social Sharing</label>
                            </li>
                            <li class="to-field">
                                <select name="audio_social_share" id="audio_social_share" class="dropdown">
                                    <option <?php if($audio_social_share_db=="Yes")echo "selected" ?> >Yes</option>
                                    <option <?php if($audio_social_share_db=="No")echo "selected" ?> >No</option>
                                </select>
                            </li>
                            <li class="to-desc">
                                <p>
                                    Make Social Sharing On/Off
                                </p>
                            </li>
                        </ul>
                    </div>
					<div class="clear"></div>
                </div>
                
                        
	        </div>
				<?php include("inc_meta_layout.php")?>
                <input type="hidden" name="audio_meta_form" value="1" />
                <div class="clear"></div>
            <div class="clear"></div>

<?php
		}

		if ( isset($_POST['audio_meta_form']) and $_POST['audio_meta_form'] == 1 ) {
			if ( empty($_POST['cs_layout']) ) $_POST['cs_layout'] = 'none';
			add_action( 'save_post', 'cs_meta_audio_save' );  
			function cs_meta_audio_save( $post_id )
			{  
				$sxe = new SimpleXMLElement("<audio></audio>");
					if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; 
					if ( empty($_POST["audio_release_date"]) ) $_POST["audio_release_date"] = "";
					if ( empty($_POST["audio_social_share"]) ) $_POST["audio_social_share"] = "";
					if ( empty($_POST["audio_buy_amazon"]) ) $_POST["audio_buy_amazon"] = "";
					if ( empty($_POST["audio_buy_apple"]) ) $_POST["audio_buy_apple"] = "";
					if ( empty($_POST["audio_buy_groov"]) ) $_POST["audio_buy_groov"] = "";
					if ( empty($_POST["audio_buy_cloud"]) ) $_POST["audio_buy_cloud"] = "";
						$sxe = save_layout_xml($sxe);
								$sxe->addChild('audio_release_date', $_POST['audio_release_date'] );
								$sxe->addChild('audio_social_share', $_POST['audio_social_share'] );
								$sxe->addChild('audio_buy_amazon', $_POST['audio_buy_amazon'] );
								$sxe->addChild('audio_buy_apple', $_POST['audio_buy_apple'] );
								$sxe->addChild('audio_buy_groov', $_POST['audio_buy_groov'] );
								$sxe->addChild('audio_buy_cloud', $_POST['audio_buy_cloud'] );
							$counter = 0;
							if ( isset($_POST['audio_track_title']) ) {
								foreach ( $_POST['audio_track_title'] as $count ){
									$track = $sxe->addChild('track');
										$track->addChild('audio_track_title', $_POST['audio_track_title'][$counter] );
										$track->addChild('audio_track_mp3_url', $_POST['audio_track_mp3_url'][$counter] );
										$track->addChild('audio_track_playable', $_POST['audio_track_playable'][$counter] );
										$track->addChild('audio_track_downloadable', $_POST['audio_track_downloadable'][$counter] );
										$track->addChild('audio_track_lyrics', $_POST['audio_track_lyrics'][$counter] );
										$track->addChild('audio_track_buy_mp3', $_POST['audio_track_buy_mp3'][$counter] );
										$counter++;
								}
							}
				update_post_meta( $post_id, 'cs_audio', $sxe->asXML() );
			}
		}
		// adding audio meta info end
	
// audio end